<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
        :before-open="inputBeforeOpen"
        @selection-change="selectionChange"
        @cell-click="cellClick"
      >
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.downloadBtn"
            @click.stop="handleDownload"
          >下 载
          </el-button>
          <el-button type="primary" size="small" @click="handleAdd"
          >添 加
          </el-button>
          <el-button type="primary" size="small" @click="handleExcelWrite"
          >导 出
          </el-button>
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.inputApplyBtn"
            @click.stop="inputApply()"
            >入库修改提交
          </el-button>
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.inputApproveBtn"
            @click.stop="inputApprove()"
            >入库修改审批
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="inputBatchComplete()"
          >入库操作
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="recycleInputBatchComplete()"
          >回收入库操作
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="delBatchs()"
          >删除
          </el-button>
	  <el-button
            type="primary"
            size="small"
	    v-permission="permissionList.delBtn"
            @click="superDelBatchs()"
          >高级删除
          </el-button>
          <el-button
            type="primary"
            size="small"
            :hidden="true"
            v-if="false"
            @click.stop="handlePrint"
          >打 印
          </el-button>
        </template>
        <template slot="menuForm" slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click.stop="handleKeeper2Confirm"
          >库管2确认
          </el-button>
        </template>
        <template slot="menu" slot-scope="{ row, index }">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="row.checkApproveStatus=='3'"
            v-permission="permissionList.editBtn"
            @click="rowEdit(row, index)"
          >编辑
          </el-button>
<!--          <el-button-->
<!--            type="text"-->
<!--            size="small"-->
<!--            icon="el-icon-document-copy"-->
<!--            v-if="row.dataSource=='0'||row.dataSource=='1'||row.dataSource=='2'||row.dataSource=='3'"-->
<!--            v-permission="permissionList.completeBtn"-->
<!--            @click="rowCompleteInfo(row, index)"-->
<!--          >完善信息-->
<!--          </el-button>-->
          <el-button
            type="text"
            size="small"
            icon="el-icon-tickets"
            v-if="row.breakFlag == '0'"
            @click="handleDecompose(row, index)"
          >分 解
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-tickets"
            v-if="row.breakFlag == '1'"
            @click="handleDecomposeDetail(row, index)"
          >分解明细
          </el-button>
          <!-- 入库类型为测试入库且库管2为空时进行确认 -->
          <el-button
            type="text"
            size="small"
            icon="el-icon-check"
            v-if="row.inGoingType == '4' && !row.keeper2code"
            @click="handleConfirm(row, index)"
          >确 认
          </el-button>
        </template>
      </avue-crud>
      <open-dialog title="回收入库操作" :DialogVisible.sync="recycleBatchCompleteVisableDialog">
        <el-form label-width="60px">
          <avue-form
            v-model="recycleInputForm.swStInputDetail"
            :option="recycleTableOptionComplete"
            ref="recycleBatchCompleteforms"
          >
          </avue-form>
          <div class="bottom-menu-complete">
            <el-button type="primary" size="small" @click="recycleInputDetailComplete()">保存</el-button>
            <el-button type="primary" size="small" @click="recycleBatchCompleteVisableDialog = false"
            >取消</el-button
            >
            <el-button
              type="primary"
              size="small"
              @click.stop="recycleKeeper2BatchConfirmsComplete"
            >库管2确认
            </el-button>
          </div>
        </el-form>
      </open-dialog>
      <open-dialog title="完善信息" :DialogVisible.sync="completeVisableDialog">
        <el-form label-width="60px">
          <avue-form
            v-model="formComplete.swStInputDetail"
            :option="tableOptionComplete"
            ref="completeforms"
          >
          </avue-form>
          <div class="bottom-menu-complete">
            <el-button type="primary" size="small" @click="inputDetailComplete()">保存</el-button>
            <el-button type="primary" size="small" @click="completeVisableDialog = false"
              >取消</el-button
            >
            <el-button
                type="primary"
                size="small"
                @click.stop="keeper2ConfirmsComplete"
              >库管2确认
            </el-button>
          </div>
        </el-form>
      </open-dialog>
      <open-dialog title="添加" :DialogVisible.sync="addVisableDialog">
        <el-form label-width="60px">
          <avue-form
            ref="inputForms"
            v-model="formAdd.swStInputDetail"
            :option="tableOptionAdd"
          ></avue-form>
          <dynamic-crud
            title="入库明细"
            :data="formAdd.swStInputDetailList"
            :option="tableOptionAddDetail"
            :rowAdd="rowadd"
          ></dynamic-crud>
          <div class="bottom-menu-input">
            <el-button type="primary" size="small" @click="inputDetailSave()">保存</el-button>
            <el-button type="primary" size="small" @click="addVisableDialog = false"
              >取消</el-button
            >
            <el-button
                type="primary"
                size="small"
                @click.stop="keeper2Confirms"
              >库管2确认
            </el-button>
          </div>
        </el-form>
      </open-dialog>
      <open-dialog title="分 解" :DialogVisible.sync="dialogVisible">
        <div class="header-menu" v-if="detail">
          <el-button type="primary" size="small" v-print="'#codeList'"
          >生成条形码
          </el-button
          >
        </div>
        <dynamic-crud
          title="入库单信息"
          :data="[inputForm]"
          :option="tableOption"
          :button="false"
        >
        </dynamic-crud>
        <dynamic-crud
          title="入库单分解明细"
          :data="decomposeDetail"
          :option="boxTableOption"
          :button="!detail"
        >
        </dynamic-crud>
        <template slot="footer" v-if="!detail">
          <el-button type="primary" size="small" @click="decomposeSubmit"
          >确 认
          </el-button
          >
          <el-button size="small" @click="dialogVisible = false"
          >取 消
          </el-button
          >
        </template>
        <div v-if="detail" v-show="false">
          <div ref="codeList" id="codeList">
            <!--style="page-break-after:always" 表示添加分页功能  -->
            <div
              v-for="deco in codelists"
              :key="deco.keys"
              style="page-break-after: always"
            >
              <img
                :src="deco.code"
                class="bar-code"
              />
            </div>
          </div>
        </div>
      </open-dialog>
      <open-dialog title="入库操作" :DialogVisible.sync="batchCompleteVisableDialog">
        <el-form label-width="60px">
          <avue-form
            v-model="formBatchComplete.swStInputDetail"
            :option="tableOptionBatchComplete"
            ref="batchCompleteforms"
          >
          </avue-form>
          <div class="bottom-menu-complete">
            <el-button type="primary" size="small" @click="inputDetailBatchComplete()">保存</el-button>
            <el-button type="primary" size="small" @click="batchCompleteVisableDialog = false"
              >取消</el-button
            >
            <el-button
                type="primary"
                size="small"
                @click.stop="keeper2BatchConfirmsComplete"
              >库管2确认
            </el-button>
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
  import baseCrud from "@/mixins/base-crud";
  import {handleDownloadZip} from "@/util/util.js";
  import {dateFormat} from "@/util/date";
  import {checkPwd} from "@/api/admin/user";
  import store from "@/store";
  import {handleExcelResponse} from "@/util/util";
  import {boxTableOption,tableOptionAdd,tableOptionAddDetail,tableOptionComplete,recycleTableOptionComplete,tableOptionBatchComplete,startDate, endDate} from "@/const/crud/subway/swstinputdetail";
  import {
    fetchList,
    addBoxObj,
    getBoxDetail,
    exportByIds,
    exportAll,
    inventoryInType,
    addObj,applyed,approved,getCodeListDetail,putObj,putBatchObj,delBatch
  } from "@/api/subway/swstinputdetail";
  import {validatenull} from "@/util/validate";

  export default {
    name: "SwStIntputDetail",
    mixins: [baseCrud("subway/swstinputdetail")],
    data() {
      return {
        tableOption: {selection: true},
        exportName: "车票入库.xlsx",
        dialogVisible: false,
        inputForm: {},
        detail: false,
        decomposeDetail: [],
        codelists: [],
        selectKeys: [], //存放对像
        selectKeyVals: [], //存放值
        tableOptionAdd: tableOptionAdd,
        tableOptionAddDetail: tableOptionAddDetail,
        tableOptionComplete: tableOptionComplete,
        recycleTableOptionComplete: recycleTableOptionComplete,
        tableOptionBatchComplete: tableOptionBatchComplete,
        formAdd:{
          swStInputDetail: {},
          swStInputDetailList: []
        },
        formComplete:{
          swStInputDetail: {}
        },
        recycleInputForm:{
          ids:[],
          swStInputDetail:{}
        },
        formBatchComplete:{
          ids:[],
          swStInputDetail: {}
        },
        addVisableDialog: false,
        completeVisableDialog: false,
        batchCompleteVisableDialog: false,
        recycleBatchCompleteVisableDialog: false,
        isKeeper2Confirm: false,
        searchForm: {
          dates1: [startDate, endDate],
        }
      };
    },
    computed: {
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.st_input_detail_add, false),
          delBtn: this.vaildData(this.permissions.st_input_detail_delete, false),
          editBtn: this.vaildData(this.permissions.st_input_detail_update, false),
          completeBtn: this.vaildData(this.permissions.st_input_detail_complete,false),
          downloadBtn: this.vaildData(
            this.permissions.st_input_detail_download,
            false
          ),
          inputApplyBtn: this.vaildData(this.permissions.st_input_detail_apply, false),
          inputApproveBtn: this.vaildData(this.permissions.st_input_detail_approve, false)
        };
      },
      boxTableOption() {
        return {
          column: [
            {
              type: "input",
              label: "入库单流水号",
              prop: "refNo",
              hide: !this.detail,
            },
            {
              type: "input",
              label: "箱子流水号",
              prop: "boxNo",
              hide: !this.detail,
            },
            {
              type: "number",
              label: "数量",
              prop: "ticketNumber",
              cell: true,
            },
            {
              type: "input",
              label: "票卡卡号",
              prop: "ticketNo",
              cell: true,
            },
          ],
        };
      },
    },
    watch: {
      "form.keeper2code"(newValue, oldValue) {
        // 库管2变化后将确认状态置为false
        this.form.keeper2Confirmed = false;
      },
      "formAdd.swStInputDetail.keeper2code"(newValue, oldValue) {
        // 库管2变化后将确认状态置为false
        this.isKeeper2Confirm = false;
      },
      "formComplete.swStInputDetail.stockArea"(val){
        //获取渲染列
        var stockArea = this.findObject(this.tableOptionComplete.group[0].column, "stockArea");
        //如果库区不为空，不允许修改
        if (val != "" && val != null && val != undefined) {
          stockArea.disabled = true
        }else{
          stockArea.disabled = false
        }
      }
    },
    methods: {
      //删除操作
      delBatchs: function () {
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据！");
          return false;
        }
        let ids = [];
        for (const table of this.$refs.crud.tableSelect) {
          if (table.stockArea != "" && table.stockArea != null && table.stockArea != undefined) {
            this.$message.error("请检查数据，只能选择无库区的数据！");
            return false;
          }
          ids.push(table.id);
        }
        this.$confirm("确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delBatch(ids)
            .then((data) => {
              this.$message.success("删除成功");
              this.getList(this.page);
              done();
            })
            .catch(() => {
              loading();
            });
        });
      },
       //高级删除操作
      superDelBatchs: function () {
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据！");
          return false;
        }
        let ids = [];
        for (const table of this.$refs.crud.tableSelect) {
          ids.push(table.id);
        }
        this.$confirm("确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delBatch(ids)
            .then((data) => {
              this.$message.success("删除成功");
              this.getList(this.page);
              done();
            })
            .catch(() => {
              loading();
            });
        });
      },
      //回收入库操作
      recycleInputBatchComplete(){
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据");
          return false;
        }
        let ticketReturnType = '';
        for (const table of this.$refs.crud.tableSelect) {
          if (table.ticketReturnType == "" || table.ticketReturnType == null || table.ticketReturnType == undefined) {
            if (table.stockArea != "" && table.stockArea != null && table.stockArea != undefined) {
              this.$message.error("请检查数据，只能选择无库区的数据！");
              return false;
            }
          }
          if (table.stockArea != "" && table.stockArea != null && table.stockArea != undefined) {
            this.$message.error("请检查数据，只能选择无库区的数据！");
            return false;
          }
          if (table.inGoingType != '6') {
            this.$message.error("请检查数据，只能选择回收的数据！");
            return false;
          }
          if (ticketReturnType == '') {
            ticketReturnType = table.ticketReturnType;
          }
          // if (table.ticketReturnType != ticketReturnType) {
          //   this.$message.error("请检查数据，回收入库操作只能选择相同车票种类的数据！");
          //   return false;
          // }
          this.recycleInputForm.swStInputDetail = table;
        }
        for (const table of this.$refs.crud.tableSelect) {
          if ((table.stockArea == "" || table.stockArea == null) && table.inGoingType == '6') {
            this.recycleInputForm.swStInputDetail.stockArea = table.stockAreaTemorary
          }
        }
        this.recycleInputForm.ids = [];
        // this.formBatchComplete.ids = [];
        // for (var i in this.formBatchComplete.swStInputDetail) {
        //   this.formBatchComplete.swStInputDetail[i] = null;
        // }
        this.isKeeper2Confirm = false;
        this.recycleBatchCompleteVisableDialog = true;
      },
      //入库操作
      inputBatchComplete(){
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据");
          return false;
        }
        for (const table of this.$refs.crud.tableSelect) {
          if (table.ticketReturnType != "" && table.ticketReturnType != null && table.ticketReturnType != undefined) {
            this.$message.error("请检查数据，入库操作不能操作回收的数据！");
            return false;
          }
          if (table.stockArea != "" && table.stockArea != null && table.stockArea != undefined) {
            this.$message.error("请检查数据，只能选择无库区的数据！");
            return false;
          }
        }
        this.formBatchComplete.ids = [];
        for (var i in this.formBatchComplete.swStInputDetail) {
          this.formBatchComplete.swStInputDetail[i] = null;
        }
        this.isKeeper2Confirm = false;
        this.batchCompleteVisableDialog = true;
      },
      recycleInputDetailComplete(){
        for (const table of this.$refs.crud.tableSelect) {
          this.recycleInputForm.ids.push(table.id);
        }
        this.$refs.recycleBatchCompleteforms.validate(vaild=>{
          if(vaild){
            if(!this.isKeeper2Confirm){
              this.$message.warning("请先进行库管2确认");
              return;
            }
            putBatchObj(this.recycleInputForm).then(data => {
              this.$message.success("操作成功");
              this.getList(this.page);
              this.recycleBatchCompleteVisableDialog = false;
              done();
            })
              .catch(() => {});
          }
        })
      },
      inputDetailBatchComplete(){
        for (const table of this.$refs.crud.tableSelect) {
          this.formBatchComplete.ids.push(table.id);
        }
        this.$refs.batchCompleteforms.validate(vaild=>{
          if(vaild){
            if(!this.isKeeper2Confirm){
              this.$message.warning("请先进行库管2确认");
              return;
            }
            putBatchObj(this.formBatchComplete).then(data => {
                this.$message.success("操作成功");
                this.getList(this.page);
                this.batchCompleteVisableDialog = false;
                done();
              })
              .catch(() => {});
          }
        })
      },
      rowCompleteInfo(row){
        this.isKeeper2Confirm = false;
        this.formComplete.swStInputDetail = row;
        // if (!validatenull(row.ticketReturnType) && !validatenull(row.stockAreaTemorary)) {
        //   this.formComplete.swStInputDetail.stockArea = this.formComplete.swStInputDetail.stockAreaTemorary
        // }
        this.completeVisableDialog = true;
      },
      inputDetailComplete(){
        this.$refs.completeforms.validate(vaild=>{
          if(vaild){
            if(!this.isKeeper2Confirm){
              this.$message.warning("请先进行库管2确认");
              return;
            }
            let inGoingType = this.formComplete.swStInputDetail.inGoingType;
            let ticketReturnType = this.formComplete.swStInputDetail.ticketReturnType;
            let stockAreaTemorary = this.formComplete.swStInputDetail.stockAreaTemorary;
            let stockArea = this.formComplete.swStInputDetail.stockArea;
            if (inGoingType == '6'
              && ticketReturnType != null
              && stockAreaTemorary != null) {
              if (stockAreaTemorary != stockArea) {
                if (stockAreaTemorary == '8') {
                  this.$message.warning("回收车票库区应选择'待清洗区'");
                  return;
                } else if (stockAreaTemorary == '3') {
                  this.$message.warning("回收车票库区应选择'已编码区'");
                  return;
                } else if (stockAreaTemorary == '10') {
                  this.$message.warning("回收车票库区应选择'待销毁区'");
                  return;
                }
              }
            }
            putBatchObj(this.formComplete).then(data => {
                this.$message.success("完善成功");
                this.getList(this.page);
                this.completeVisableDialog = false;
                done();
              })
              .catch(() => {});
          }
        })
      },
      inputApply: function() {
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据");
          return false;
        }
        let ids = [];
        for (const table of this.$refs.crud.tableSelect) {
          if (table.checkApproveStatus != "1" && table.checkApproveStatus != "4") {
            this.$message.error("请检查数据，只能选择审批状态为未申请或审批未通过的数据进行申请！");
            return false;
          }
          ids.push(table.id);
        }
        this.$confirm("确认提交?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          applyed(ids)
            .then(data => {
              this.$message.success("申请成功");
              this.getList(this.page);
              done();
            })
            .catch(() => {});
        });
      },
      inputApprove: function() {
        if (this.$refs.crud.tableSelect.length < 1) {
          this.$message.error("请选择数据");
          return false;
        }
        let ids = [];
        for (const table of this.$refs.crud.tableSelect) {
          if (table.checkApproveStatus != "2") {
            this.$message.error("请检查数据，只能选择审批状态为已申请的数据进行审批！");
            return false;
          }
          ids.push(table.id);
        }
        this.$confirm("确认提交?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          approved(ids)
            .then(data => {
              this.$message.success("日计划审批成功");
              this.getList(this.page);
              done();
            })
            .catch(() => {});
        });
      },
      handleAdd(){
        for (var i in this.formAdd.swStInputDetail) {
          this.formAdd.swStInputDetail[i] = null;
        }
        this.formAdd.swStInputDetailList = [];
        this.isKeeper2Confirm = false;
        let now = new Date();
        this.formAdd.swStInputDetail.inGoingTime = dateFormat(now);
        this.addVisableDialog = true;
      },
      inputDetailSave(){
        this.$refs.inputForms.validate(vaild=>{
          if(vaild){
            if (this.formAdd.swStInputDetailList.length==0) {
              this.$message.warning("请填写入库明细信息！");
              return;
            }
            for (const item of this.formAdd.swStInputDetailList) {
              if (
                this.validatenull(item.ticketType) ||
                this.validatenull(item.ticketName) ||
                this.validatenull(item.ticketNumber) ||
                this.validatenull(item.stockArea)
              ) {
              // ||
              //   this.validatenull(item.latestStartTime)
                this.$message.info(
                  "入库明细中的票种、车票名称、车票数量、库区分区不能为空！"
                );
                return;
              }
            }
            if(!this.isKeeper2Confirm){
              this.$message.warning("请先进行库管2确认");
              return;
            }
            addObj(this.formAdd)
              .then((data) => {
                this.$message.success("添加成功");
                this.getList(this.page);
                this.addVisableDialog = false;
                done();
              })
              .catch(() => {});
          }
        })
      },
      keeper2ConfirmsComplete() {
        if (!this.formComplete.swStInputDetail.keeper2code) {
          this.$message.info("请先选择库管2");
          return;
        }
        // if (this.formComplete.swStInputDetail.keeper2code == store.getters.userInfo.userCode) {
        //   this.$message.error("请检查库管2，不能本人进行确认！");
        //   return false;
        // }
        this.$prompt("请输入密码", "确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputType: "password",
          inputValidator: (val) => {
            if (!val) return "请输入密码";
          },
        })
          .then(({ value }) => {
            checkPwd(this.formComplete.swStInputDetail.keeper2code, value).then((response) => {
              var result = response.data.data;
              if (result) {
                this.isKeeper2Confirm = true;
                this.$message.success("确认成功");
              } else {
                this.$message.error("密码错误");
              }
            });
          })
          .catch(() => {});
      },
      keeper2Confirms() {
        if (!this.formAdd.swStInputDetail.keeper2code) {
          this.$message.info("请先选择库管2");
          return;
        }
        // if (this.formAdd.swStInputDetail.keeper2code == store.getters.userInfo.userCode) {
        //   this.$message.error("请检查库管2，不能本人进行确认！");
        //   return false;
        // }
        this.$prompt("请输入密码", "确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputType: "password",
          inputValidator: (val) => {
            if (!val) return "请输入密码";
          },
        })
          .then(({value}) => {
            checkPwd(this.formAdd.swStInputDetail.keeper2code, value).then((response) => {
              var result = response.data.data;
              if (result) {
                this.isKeeper2Confirm = true;
                this.$message.success("确认成功");
              } else {
                this.$message.error("密码错误");
              }
            });
          })
          .catch(() => {
          });
      },
      recycleKeeper2BatchConfirmsComplete() {
        if (!this.recycleInputForm.swStInputDetail.keeper2code) {
          this.$message.info("请先选择库管2");
          return;
        }
        this.$prompt("请输入密码", "确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputType: "password",
          inputValidator: (val) => {
            if (!val) return "请输入密码";
          },
        })
          .then(({ value }) => {
            checkPwd(this.recycleInputForm.swStInputDetail.keeper2code, value).then((response) => {
              var result = response.data.data;
              if (result) {
                this.isKeeper2Confirm = true;
                this.$message.success("确认成功");
              } else {
                this.$message.error("密码错误");
              }
            });
          })
          .catch(() => {});
      },
      keeper2BatchConfirmsComplete() {
        if (!this.formBatchComplete.swStInputDetail.keeper2code) {
          this.$message.info("请先选择库管2");
          return;
        }
        this.$prompt("请输入密码", "确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputType: "password",
          inputValidator: (val) => {
            if (!val) return "请输入密码";
          },
        })
          .then(({ value }) => {
            checkPwd(this.formBatchComplete.swStInputDetail.keeper2code, value).then((response) => {
              var result = response.data.data;
              if (result) {
                this.isKeeper2Confirm = true;
                this.$message.success("确认成功");
              } else {
                this.$message.error("密码错误");
              }
            });
          })
          .catch(() => {});
      },
      rowadd(callback){
        callback()
      },
      selectionChange(list){
        this.selectKeys = list;
        this.selectKeyVals = [];
        for (let i = 0; i < list.length; i++) {
          this.selectKeyVals.push(list[i].id);
        }
      },
      inputBeforeOpen(done, type) {
        if (type == "add") {
          setTimeout(() => {
            (this.form.inGoingTime = dateFormat(new Date())),
              //给附件置空
              (this.form.filePath = "");
          });
        }
        done();
      },
      getList(page) {
        this.tableLoading = true;
        return fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize,
            },
            this.params,
            this.searchForm
          )
        ).then((response) => {
          this.tableData = response.data.data.records;
          this.tableData.forEach((row) => {
            if(!this.validatenull(row.stockArea)){
              row.stockArea = Number(row.stockArea);
            }
            if(!this.validatenull(row.stockAreaTemorary)){
              row.stockAreaTemorary = Number(row.stockAreaTemorary);
            }
          });
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        });
      },
      beforeRowSave(row, callback, loading) {
        if (!row.keeper2Confirmed) {
          this.$message.info("请先进行库管2确认");
          loading();
        } else {
          callback();
        }
      },
      beforeRowUpdate(row, index, callback, loading) {
        if (!row.keeper2Confirmed) {
          this.$message.info("请先进行库管2确认");
          loading();
        } else {
          callback();
        }
      },
      cellClick(row, column, cell, event) {
        if (column.property == "ticketNo") {
          this.$alert(row.ticketNo);
        }
      },
      handleKeeper2Confirm() {
        if (!this.form.keeper2code) {
          this.$message.info("请先选择库管2");
          return;
        }
        // if (this.form.keeper2code == store.getters.userInfo.userCode) {
        //   this.$message.error("请检查库管2，不能本人进行确认！");
        //   return false;
        // }
        this.$prompt("请输入密码", "确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputType: "password",
          inputValidator: (val) => {
            if (!val) return "请输入密码";
          },
        })
          .then(({value}) => {
            checkPwd(this.form.keeper2code, value).then((response) => {
              var result = response.data.data;
              if (result) {
                this.$message.success("确认成功");
              } else {
                this.$message.error("密码错误");
              }
              this.form.keeper2Confirmed = result;
            });
          })
          .catch(() => {
          });
      },
      handleDecompose(row, index) {
        this.detail = false;
        this.inputForm = row;
        this.decomposeDetail = [
          {
            $cellEdit: true,
            ticketNumber: this.inputForm.ticketNumber,
            ticketNo: this.inputForm.ticketNo,
          },
        ];
        this.dialogVisible = true;
      },
      decomposeSubmit() {
        if (this.decomposeDetail.length === 0) {
          this.$message.info("请添加分解明细");
          return;
        }
        let totalNumber = 0;
        for (let deco of this.decomposeDetail) {
          totalNumber += deco.ticketNumber;
        }
        if (this.inputForm.ticketNumber != totalNumber) {
          this.$message.info("分解数量和入库单数量不一致,请检查");
          return;
        }
        addBoxObj(this.inputForm.instockno, this.decomposeDetail).then(
          (response) => {
            this.$message.success("分解成功");
            this.getList(this.page);
            getBoxDetail(this.inputForm.instockno).then((response) => {
              this.decomposeDetail = response.data.data;
              this.detail = true;
            });
          }
        );
      },
      handleDecomposeDetail(row, index) {
        this.detail = true;
        this.inputForm = row;
        getBoxDetail(this.inputForm.instockno).then((response) => {
          this.decomposeDetail = response.data.data;
          this.getCodeList(this.decomposeDetail);
          this.dialogVisible = true;
        });
      },
      getCodeList(array){
        this.codelists = [];
        for (let index = 0; index < array.length; index++) {
          getCodeListDetail(array[index].id)
            .then(response => {
                return 'data:image/png;base64,' + btoa(
                new Uint8Array(response.data)
                    .reduce((data, byte) => data + String.fromCharCode(byte), '')
                );
            }).then(data => {
                this.codelists.push({'code':data});
                this.codelists[index].keys = index;
            });
        }
      },
      handleConfirm(row, index) {
        this.$refs.crud.rowEdit(row, index);
      },
      handleDownload() {
        let pathList = [];
        this.tableData.forEach((row) => {
          if (!this.validatenull(row.filePath)) {
            pathList.push(row.filePath);
          }
          if (!this.validatenull(row.picturePath)) {
            pathList.push(row.picturePath);
          }
        });
        if (pathList.length == 0) {
          this.$message.info("无附件");
          return;
        }
        handleDownloadZip(pathList, "车票入库附件.zip");
      }, async handleExcelWrite(){ // 导出
        if (this.selectKeys.length > 0) {//导出选中
          let reqdata = this.selectKeys;
          reqdata.forEach((row) => {
            row.inGoingType = row.$inGoingType;
            row.stockArea = row.$stockArea;
            row.ticketName = row.$ticketName;
            row.ticketType = row.$ticketType;
          })
          let p = JSON.stringify(reqdata);
          let res = await exportByIds(p);
          handleExcelResponse(res, "车票入库.xlsx");
        } else {//导出搜索全部
          let reqdata = [];
          let inventorys=[];
          await inventoryInType().then((response) => {
            inventorys = response.data.data;
          });
          await fetchList(
            Object.assign(
              {
                current: 1,
                size: 999999999,
              },
              this.params,
              this.searchForm
            )
          ).then((response) => {
            reqdata = response.data.data.records;
          });
          if (null != reqdata && reqdata.length >  0) {
            let p = {};
            p['swStInputDetailExcelBean'] = reqdata;
            p['sysDictItemBean'] = inventorys;
            let res =  await exportAll(p);
            handleExcelResponse(res, "车票入库.xlsx");
          }else {
            this.$message.warning("无导出数据");
          }

        }
      },
    },
  };
</script>

<style lang="scss" scoped>
  .header-menu {
    text-align: right;
    padding: 5px;
  }

  .bar-code {
    width: 360px;
    height: 180px;
  }
  .bottom-menu-input {
    margin-top: 48px;
    text-align: right;
  }
  .bottom-menu-complete {
    margin-top: 5px;
    text-align: right;
  }
</style>
